﻿Imports System
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Threading

Public MustInherit Class Item
    Implements INotifyPropertyChanged
    Implements IEquatable(Of Item)

    Public Sub New(ByVal ItemType As ItemType, ByVal ItemID As String, ByVal ItemName As String)
        Me.m_Type = ItemType
        Me.m_id = ItemID
        Me.m_Name = ItemName
        m_relationships = New ObservableCollection(Of Relationship)
    End Sub

    'Public Sub New(ByVal Project As Project)
    '    Me.m_Value = Project
    '    Me.m_Type = ItemType.Project
    '    Me.m_id = Project.Id.ToString
    '    Me.m_Name = Project.Name
    'End Sub

    Private m_id As String
    Private m_Name As String
    Private m_relationships As ObservableCollection(Of Relationship)
    Private m_Type As ItemType

    ''' <summary>
    ''' Gets or sets the unique identifier for each Item.
    ''' </summary>
    Public Property Id() As String
        Get
            Return m_id
        End Get
        Set(ByVal value As String)
            If Not m_id = value Then
                m_id = value
                OnPropertyChanged("Id")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the Name for each Item.
    ''' </summary>
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            If Not m_Name = value Then
                m_Name = value
                OnPropertyChanged("Name")
            End If
        End Set
    End Property

    Public ReadOnly Property Type() As ItemType
        Get
            Return m_Type
        End Get
    End Property

    ''' <summary>
    ''' Collections of relationship connection for the Item
    ''' </summary>
    Public ReadOnly Property Relationships() As ObservableCollection(Of Relationship)
        Get
            Return m_relationships
        End Get
    End Property

    ''' <summary>
    ''' Accessor for the Item's children
    ''' </summary>
    Public ReadOnly Property Children() As Collection(Of Item)
        Get
            Dim Selection As New Collection(Of Item)

            For Each rel In Relationships
                If rel.RelationshipType = RelationshipType.Child Then
                    Selection.Add(rel.RelationTo)
                End If
            Next
            Return Selection
        End Get
    End Property

    ''' <summary>
    ''' Accessor for all of the Item's parents
    ''' </summary>
    Public ReadOnly Property Parents() As Collection(Of Item)
        Get
            Dim selection As New Collection(Of Item)

            For Each rel In Relationships
                If rel.RelationshipType = RelationshipType.Parent Then
                    selection.Add(rel.RelationTo)
                End If
            Next
            Return selection
        End Get
    End Property

    ''' <summary>
    ''' Accessor for the Item's siblings
    ''' </summary>
    Public ReadOnly Property Siblings() As Collection(Of Item)
        Get
            Dim Selection As New Collection(Of Item)
            For Each rel In Relationships
                If rel.RelationshipType = RelationshipType.Sibling Then
                    Selection.Add(rel.RelationTo)
                End If

            Next

            Return Selection
        End Get
    End Property

    ''' <summary>
    ''' Accessor for the item's spouses
    ''' </summary>
    Public ReadOnly Property Spouses() As Collection(Of Item)
        Get
            Dim Selection As New Collection(Of Item)
            ' TODO: Support Spouces

            Return Selection
        End Get
    End Property

    ''' <summary>
    ''' Accessor for all of the Item's parents
    ''' </summary>
    Public ReadOnly Property WorkItems() As Collection(Of Item)
        Get
            Dim selection As New Collection(Of Item)
            For Each rel In Relationships
                If rel.RelationshipType = RelationshipType.ChildWorkItem Then
                    selection.Add(rel.RelationTo)
                End If
            Next
            Return selection
        End Get
    End Property


#Region " INotifyPropertyChanged "

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Public Sub OnPropertyChanged(ByVal PropertyName As String)
        If Not PropertyChangedEvent Is Nothing Then
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
        End If
    End Sub

#End Region

#Region " IEquatable(Of TItem) "

    Public Shadows Function Equals(ByVal other As Item) As Boolean Implements System.IEquatable(Of Item).Equals
        Return other.Id = Me.Id
    End Function

#End Region

End Class
